package com.cheyun.netsalev3.repository

import androidx.paging.PageKeyedDataSource
import com.cheyun.netsalev3.bean.EditDriveParam
import com.cheyun.netsalev3.bean.IsPhoneParam
import com.cheyun.netsalev3.bean.drive.ReserveInfo
import com.cheyun.netsalev3.bean.drive.Reservemit
import com.cheyun.netsalev3.bean.task.TaskListData
import com.cheyun.netsalev3.bean.task.TaskListParam
import com.cheyun.netsalev3.utils.CommonFunc
import com.cheyun.netsalev3.utils.CommonFunc.typeToken
import com.cheyun.netsalev3.utils.api.*
import com.google.gson.Gson
import io.reactivex.android.schedulers.AndroidSchedulers
import io.reactivex.schedulers.Schedulers
import java.util.*


class TaskListActivityRepository (var map: Map<String, String> = HashMap(),var enable:Boolean = true) : BasePageRepository<TaskListData>() {
    fun changeMap(newMap:HashMap<String, String>){
        this.map = newMap
    }
    fun setEenbleLoad(e:Boolean){
        this.enable = e
    }

    override fun loadInitial(params: PageKeyedDataSource.LoadInitialParams<Int>, callback: (data: MutableList<TaskListData>) -> Unit) {
        ApiClient.getInstance().getTaskList(1,map)
                .subscribeOn(Schedulers.io())
                .subscribeOn(AndroidSchedulers.mainThread())
                .subscribe(object : ApiCallback<BaseResp<TaskListParam>>() {
                    override fun onSuccess(t: BaseResp<TaskListParam>) {
                        t.data?.rows?.apply {
                            total.postValue(t.data!!.total)
                            this.forEach{
//                                it.hillenBtn = if(it.appstate == 4 || it.cararrive == 0) 1 else 0
                            }
                            callback(this)
                        }
                    }

                    override fun onError(ex: ApiException) {
                        networkState.postValue(NetworkState.error(ex.getMessage()))
                    }

                })
    }


    override fun loadAfter(params: PageKeyedDataSource.LoadParams<Int>, callback: PageKeyedDataSource.LoadCallback<Int, TaskListData>) {

        ApiClient.getInstance().getTaskList(params.key, map)
                .subscribeOn(Schedulers.io())
                .subscribeOn(AndroidSchedulers.mainThread())
                .subscribe(object : ApiCallback<BaseResp<TaskListParam>>() {
                    override fun onSuccess(t: BaseResp<TaskListParam>) {
                        t.data?.rows?.apply {
                            this.forEach{
//                                it.hillenBtn = if(it.darrivetime.isNotEmpty()  && it.infirmity == 0) 1 else 0
//                                it.hillenBtn = if(it.appstate == 4 || it.cararrive == 0) 1 else 0
                            }
                            callback.onResult(this, params.key + 1)
                        }
                    }

                    override fun onError(ex: ApiException) {

                    }


                })
    }

    override fun loadBefore(params: PageKeyedDataSource.LoadParams<Int>, callback: PageKeyedDataSource.LoadCallback<Int, TaskListData>) {

        ApiClient.getInstance().getTaskList(params.key, map)
                .subscribeOn(Schedulers.io())
                .subscribeOn(AndroidSchedulers.mainThread())
                .subscribe(object : ApiCallback<BaseResp<TaskListParam>>() {
                    override fun onSuccess(t: BaseResp<TaskListParam>) {
                        t.data?.rows?.apply {
                            this.forEach{
//                                it.hillenBtn = if(it.appstate == 4 || it.cararrive == 0) 1 else 0
                            }
                            callback.onResult(this, params.key - 1)
                        }
                    }

                    override fun onError(ex: ApiException) {

                    }


                })
    }

}